package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.model.impl.LocalImpl;
import com.xca.znb.presenter.LocalPresenter;

public class AddPayLocalActivity extends AppCompatActivity {
    private ImageButton Back ;
    private EditText NewName , NewPhone , NewLocal ;
    private Switch SetDefault;
    private Button SaveLocal ;

    private String SwitchChange ;
    private LocalPresenter presenter ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_new_local);
        Back = findViewById(R.id.add_new_local_back) ;
        NewName = findViewById(R.id.local_manager_new_name) ;
        NewPhone = findViewById(R.id.local_manager_new_phone) ;
        NewLocal = findViewById(R.id.local_manager_new_local) ;
        SetDefault = findViewById(R.id.set_default_local) ;
        SaveLocal = findViewById(R.id.save_local_btn);
        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//               Intent intent = new Intent(AddPayLocalActivity.this , LocalPayActivity.class);
//               startActivity(intent);
               finish();
            }
        });
        SwitchChange = "否";
        SetDefault.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    SwitchChange = "是";
                }else {
                    SwitchChange = "否";
                }
            }
        });

        LocalImpl local = new LocalImpl(this);
        presenter = new LocalPresenter(local);

        SaveLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = NewName.getText().toString();
                String phone = NewPhone.getText().toString();
                String local = NewLocal.getText().toString() ;
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String login_phone = sharedPreferences.getString("phone" , "");
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(local)) {
                    Toast.makeText(AddPayLocalActivity.this, "所有输入框都必须填写", Toast.LENGTH_SHORT).show();
                }
                if (phone.length() != 11){
                    Toast.makeText(AddPayLocalActivity.this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!phone.matches("[1]{1}\\d{10}")){
                    Toast.makeText(AddPayLocalActivity.this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (SwitchChange.equals("是")){
                    presenter.updateLocalType(login_phone);
                    boolean isAdd = presenter.addLocal(name , phone , local , SwitchChange , login_phone);
                    if (isAdd){
                        Toast.makeText(AddPayLocalActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
//                        Intent intent = new Intent(AddPayLocalActivity.this , LocalPayActivity.class);
//                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(AddPayLocalActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    boolean isAdd = presenter.addLocal(name , phone , local , SwitchChange , login_phone);
                    if (isAdd){
                        Toast.makeText(AddPayLocalActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
//                        Intent intent = new Intent(AddPayLocalActivity.this , LocalPayActivity.class);
//                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(AddPayLocalActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
